﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using System.Windows.Browser;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class IndexWindow : UserControl
    {
        public string Index;
        public string uploadpath = App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin"));

        private Storyboard storyIn;
        private Storyboard storyOut;
        private ScaleTransform scaleIn;
        private ScaleTransform scaleOut;

        public IndexWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(IndexWindow_Loaded);

        }

        public void Close()
        {
            if (null != storyIn && storyIn.GetCurrentState() != ClockState.Stopped)
            {
                storyIn.Pause();
            }
            storyIn = null;

            storyOut = new Storyboard();
            scaleOut = new ScaleTransform();
            scaleOut.ScaleX = 1;
            scaleOut.ScaleY = 1;
            scaleOut.CenterX = 450;
            scaleOut.CenterY = 255;
            this.RenderTransform = scaleOut;

            DoubleAnimation animationX = new DoubleAnimation();
            animationX.From = 1;
            animationX.To = 0;
            animationX.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animationX, scaleOut);
            Storyboard.SetTargetProperty(animationX, new PropertyPath(ScaleTransform.ScaleXProperty));
            storyOut.Children.Add(animationX);

            DoubleAnimation animationY = new DoubleAnimation();
            animationY.From = 1;
            animationY.To = 0;
            animationY.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animationY, scaleOut);
            Storyboard.SetTargetProperty(animationY, new PropertyPath(ScaleTransform.ScaleYProperty));
            storyOut.Children.Add(animationY);
            storyOut.Begin();
        }

        void IndexWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (null != storyOut && storyOut.GetCurrentState() != ClockState.Stopped)
            {
                storyOut.Pause();
            }
            storyOut = null;

            storyIn = new Storyboard();
            scaleIn = new ScaleTransform();
            scaleIn.ScaleX = 0;
            scaleIn.ScaleY = 0;
            scaleIn.CenterX = 450;
            scaleIn.CenterY = 255;
            this.RenderTransform = scaleIn;

            DoubleAnimation animationX = new DoubleAnimation();
            animationX.From = 0;
            animationX.To = 1;
            animationX.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animationX, scaleIn);
            Storyboard.SetTargetProperty(animationX, new PropertyPath(ScaleTransform.ScaleXProperty));
            storyIn.Children.Add(animationX);

            DoubleAnimation animationY = new DoubleAnimation();
            animationY.From = 0;
            animationY.To = 1;
            animationY.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animationY, scaleIn);
            Storyboard.SetTargetProperty(animationY, new PropertyPath(ScaleTransform.ScaleYProperty));
            storyIn.Children.Add(animationY);

            storyIn.Begin();

            //this.tbTest.Text = Index;
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadIndexCompleted += new EventHandler<ReadIndexCompletedEventArgs>(client_ReadIndexCompleted);
            client.ReadIndexAsync(Convert.ToInt32(this.Index));
        }

        void client_ReadIndexCompleted(object sender, ReadIndexCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    if (e.Result.Path != "")
                    {
                        //图片
                        BitmapImage img = new BitmapImage(new Uri(uploadpath + "/" + e.Result.Path, UriKind.Absolute));
                        this.imgBk.Source = img;

                        //按钮
                        if (e.Result.Product > 0)
                        {
                            this.btnProductDetail.Visibility = Visibility.Visible;
                            this.btnProductList.Visibility = Visibility.Visible;
                            this.btnProductDetail.Tag = e.Result.Product.ToString();
                        }
                        else
                        {
                            this.btnProductList.Visibility = Visibility.Collapsed;
                            this.btnProductDetail.Visibility = Visibility.Collapsed;
                            this.btnProductDetail.Tag = null;
                        }

                        this.LayoutRoot.UpdateLayout();
                    }
                }
            }
            else
            {
                throw e.Error;
            }
        }

        private void btnProductDetail_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as Button).Tag != null)
            {
                App app = (App)Application.Current;
                ProductDetail product = new ProductDetail();
                product.ID = Convert.ToInt32((sender as Button).Tag.ToString());
                app.RedirectTo(product);
                Close();
            }
        }

        private void btnProductList_Click(object sender, RoutedEventArgs e)
        {
            HtmlWindow html = HtmlPage.Window;
            html.Navigate(new Uri("productlist.aspx", UriKind.Relative), "_blank");
            //MessageBox.Show("test");
        }


    }
}
